package com.jcrj.service;

import com.jcrj.dao.PersonalofficeDao;
import com.jcrj.entity.OAddressbook;
import com.jcrj.entity.ODaily;
import com.jcrj.entity.OFax;
import com.jcrj.entity.OFaxReceive;
import com.jcrj.entity.OGroupUser;
import com.jcrj.entity.OInfo;
import com.jcrj.entity.OMailReceiver;
import com.jcrj.entity.ONotice;
import com.jcrj.entity.OUser;
import com.jcrj.entity.OWorkGroup;
import com.jcrj.entity.OWorkLan;
import com.jcrj.util.GetText;
import java.util.List;

public class PersonalofficeService {
	private PersonalofficeDao personalofficeDao;
	private UserService userService;

	public ODaily getDailyNew(int id) {
		return (ODaily) this.personalofficeDao.getObject(new ODaily(), id);
	}

	public void updateDalily(ODaily oDaily) {
		this.personalofficeDao.ubdateText(oDaily);
	}

	public void updateUserInfo(OUser user) {
		this.personalofficeDao.ubdateText(user);
	}

	public OAddressbook getbook(int id) {
		return (OAddressbook) this.personalofficeDao.getObject(new OAddressbook(), id);
	}

	public void saveBookUser(OAddressbook oAddressbook) {
		this.personalofficeDao.saveOrUpdate(oAddressbook);
	}

	public Long countbook(String name, String policeNumber) {
		String hql = "select count(id) from OUser o where o.del=0";
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and o.name like '%" + name.trim() + "%'";
		}
		if ((policeNumber != null) && (!"".equals(policeNumber))) {
			hql = hql + " and o.account like '%" + policeNumber.trim() + "%'";
		}
		return this.personalofficeDao.countTotalByHql(hql);
	}

	public List<OUser> getbookList(String name, String policeNumber, int page, int count) {
		String hql = "from OUser o where o.del=0";
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and o.name like '%" + name.trim() + "%'";
		}
		if ((policeNumber != null) && (!"".equals(policeNumber))) {
			hql = hql + " and o.account like '%" + policeNumber.trim() + "%'";
		}
		hql = hql + " order by o.px asc";
		List<OUser> list = this.personalofficeDao.findByPageBook(hql, page, count);
		for (OUser user : list) {
			user.setOrgName(this.userService.getUserAllOrgName(user.getOrgId().intValue()));
		}
		return list;
	}

	public Long countbookUser(String name, OUser user) {
		String hql = "select count(id) from OAddressbook o where userId=" + user.getId();
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and o.name like '%" + name.trim() + "%'";
		}
		return this.personalofficeDao.countTotalByHql(hql);
	}

	public List<OAddressbook> getbookUserList(String name, OUser user, int page) {
		String hql = "from OAddressbook o where userId=" + user.getId();
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and o.name like '%" + name.trim() + "%'";
		}
		hql = hql + " order by o.id desc";
		return this.personalofficeDao.findByPage(hql, page);
	}

	public Object[][] getbookUserList(String name, OUser user) {
		String hql = "select name,sex,cellphone,phone,unit,job,mail,location from OAddressbook o where userId=" + user.getId();
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and o.name like '%" + name.trim() + "%'";
		}
		hql = hql + " order by o.id desc";
		List list = this.personalofficeDao.findByHql(hql);
		Object[][] obj = (Object[][]) null;
		if ((list != null) && (!list.isEmpty())) {
			obj = new Object[list.size()][((Object[]) list.get(0)).length];
			for (int i = 0; i < list.size(); i++) {
				Object[] o = (Object[]) list.get(i);
				for (int j = 0; j < o.length; j++) {
					if ((o[j] != null) && (!"".equals(o[j])))
						obj[i][j] = o[j];
					else {
						obj[i][j] = "";
					}
				}
			}
		}
		return obj;
	}

	public Object[][] getDailyExportList(String[] ids) {
		Object[][] obj = (Object[][]) null;
		String id = "";
		for (int m = 0; m < ids.length; m++) {
			if (id == "")
				id = id + ids[m];
			else {
				id = id + "," + ids[m];
			}
			String hql = "select to_char(createTime,'yyyy-mm-dd'),weather,content from ODaily o where id in(" + id + ") order by o.createTime desc";
			List list = this.personalofficeDao.findByHql(hql);
			obj = new Object[list.size()][((Object[]) list.get(0)).length];
			for (int i = 0; i < list.size(); i++) {
				Object[] o = (Object[]) list.get(i);
				for (int j = 0; j < o.length; j++) {
					if ((o[j] != null) && (!"".equals(o[j])))
						obj[i][j] = GetText.Html2Text(o[j].toString());
					else {
						obj[i][j] = "";
					}
				}
			}
		}
		return obj;
	}

	public void addDaily(ODaily oDaily) {
		this.personalofficeDao.saveText(oDaily);
	}

	public List<ODaily> getDaily(int id, String name, int page, String startDate, String endDate) {
		String hql = "from ODaily where userId=" + id;
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and content like '%" + name.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and createTime >= to_date('" + startDate + "','yyyy-mm-dd')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and createTime <= to_date('" + endDate + "','yyyy-mm-dd')";
		}
		hql = hql + " order by createTime desc";
		List<ODaily> list = this.personalofficeDao.findByPage(hql, page);
		for (ODaily d : list) {
			d.setContent(GetText.Html2Text(d.getContent()));
		}
		return list;
	}

	public Long countDaily(int id, String name, String startDate, String endDate) {
		String hql = "select count(id) from ODaily  where userId=" + id;
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and content like '%" + name.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and createTime >= to_date('" + startDate + "','yyyy-mm-dd')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and createTime <= to_date('" + endDate + "','yyyy-mm-dd')";
		}
		return this.personalofficeDao.countTotalByHql(hql);
	}

	public List<ODaily> emplyeeDailyList(String userId, String name, String userName, String startDate, String endDate, int page) {
		List<ODaily> list = null;
		if ((userId != null) && (!"".equals(userId))) {
			String hql = "from ODaily where userId in(" + userId + ")";
			if ((name != null) && (!"".equals(name))) {
				hql = hql + " and content like '%" + name.trim() + "%'";
			}
			if ((userName != null) && (!"".equals(userName))) {
				hql = hql + " and userName like '%" + userName.trim() + "%'";
			}
			if ((startDate != null) && (!"".equals(startDate.trim()))) {
				hql = hql + " and createTime >= to_date('" + startDate + "','yyyy-mm-dd')";
			}
			if ((endDate != null) && (!"".equals(endDate.trim()))) {
				hql = hql + " and createTime <= to_date('" + endDate + "','yyyy-mm-dd')";
			}
			hql = hql + " order by createTime desc";
			list = this.personalofficeDao.findByPage(hql, page);
			for (ODaily d : list) {
				d.setContent(GetText.Html2Text(d.getContent()));
			}
		}
		return list;
	}

	public Long countDailyEmplyee(String userId, String name, String userName, String startDate, String endDate) {
		if ((userId != null) && (!"".equals(userId))) {
			String hql = "select count(id) from ODaily  where userId in(" + userId + ")";
			if ((name != null) && (!"".equals(name))) {
				hql = hql + " and title like '%" + name.trim() + "%'";
			}
			if ((userName != null) && (!"".equals(userName))) {
				hql = hql + " and userName like '%" + userName.trim() + "%'";
			}
			if ((startDate != null) && (!"".equals(startDate.trim()))) {
				hql = hql + " and createTime >= to_date('" + startDate + "','yyyy-mm-dd')";
			}
			if ((endDate != null) && (!"".equals(endDate.trim()))) {
				hql = hql + " and createTime <= to_date('" + endDate + "','yyyy-mm-dd')";
			}
			return this.personalofficeDao.countTotalByHql(hql);
		}
		return Long.valueOf(0L);
	}

	public String getUserEmplyee(String userId) {
		String ids = "";
		String hql = "from OUser where pid is not null";
		List<OUser> list = this.personalofficeDao.findByHql(hql);
		if ((list != null) && (!list.isEmpty())) {
			for (OUser user : list) {
				String[] pids = user.getPid().split(",");
				for (int i = 0; i < pids.length; i++) {
					if (userId.equals(pids[i])) {
						if ("".equals(ids))
							ids = user.getId().toString();
						else {
							ids = ids + "," + user.getId().toString();
						}
					}
				}
			}
		}
		return ids;
	}

	public void addgroupuser(int groupId, String[] ids) {
		String hql = "delete from OGroupUser where groupId=" + groupId;
		this.personalofficeDao.executeHql(hql);
		if ((ids != null) && (ids.length > 0))
			for (int i = 0; i < ids.length; i++) {
				OGroupUser ogu = new OGroupUser();
				ogu.setGroupId(Integer.valueOf(groupId));
				ogu.setUserId(Integer.valueOf(Integer.parseInt(ids[i])));
				this.personalofficeDao.saveText(ogu);
			}
	}

	public OWorkGroup getgroup(int id) {
		return (OWorkGroup) this.personalofficeDao.getObject(new OWorkGroup(), id);
	}

	public void addgroup(OWorkGroup owg) {
		this.personalofficeDao.saveText(owg);
	}

	public OUser getpsw(int id) {
		return (OUser) this.personalofficeDao.getObject(new OUser(), id);
	}

	public void updateText(OWorkLan owLan) {
		this.personalofficeDao.ubdateText(owLan);
	}

	public List<OWorkGroup> getGroup(int id) {
		String hql = "from OWorkGroup where userid=" + id;
		return this.personalofficeDao.findByHql(hql);
	}

	public List<OWorkLan> getlang(int id, String name, int page) {
		String hql = "from OWorkLan where userId=" + id;
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and content like '%" + name.trim() + "%'";
		}
		hql = hql + " order by id desc";
		return this.personalofficeDao.findByPage(hql, page);
	}

	public Long countLang(int id, String name) {
		String hql = "select count(id) from OWorkLan where userId=" + id;
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and content like '%" + name.trim() + "%'";
		}
		return this.personalofficeDao.countTotalByHql(hql);
	}

	public List<OWorkLan> getWorkLanList(int userId) {
		String hql = "from OWorkLan where userId=" + userId;
		return this.personalofficeDao.findByHql(hql);
	}

	public OWorkLan gettext(int id) {
		return (OWorkLan) this.personalofficeDao.getObject(new OWorkLan(), id);
	}

	public void savelang(OWorkLan owLan) {
		this.personalofficeDao.saveText(owLan);
	}

	public void deletebook(String[] ids) {
		if (ids != null) {
			String id = "";
			for (int i = 0; i < ids.length; i++) {
				if (id == "")
					id = id + ids[i];
				else {
					id = id + "," + ids[i];
				}
			}
			String hql = "delete from OAddressbook where id in(" + id + ")";
			this.personalofficeDao.executeHql(hql);
		}
	}

	public void dletetext(String[] textid) {
		if (textid != null) {
			String id = "";
			for (int i = 0; i < textid.length; i++) {
				if (id == "")
					id = id + textid[i];
				else {
					id = id + "," + textid[i];
				}
			}
			String hql = "delete from OWorkLan where id in(" + id + ")";
			this.personalofficeDao.executeHql(hql);
		}
	}

	public void deleteDaily(String[] dalilyId) {
		if (dalilyId != null) {
			String id = "";
			for (int i = 0; i < dalilyId.length; i++) {
				if (id == "")
					id = id + dalilyId[i];
				else {
					id = id + "," + dalilyId[i];
				}
			}
			String hql = "delete from ODaily where id in(" + id + ")";
			this.personalofficeDao.executeHql(hql);
		}
	}

	public void deletegroupuser(int id) {
		String hql = "delete from OGroupUser where groupId=" + id;
		this.personalofficeDao.executeHql(hql);
	}

	public List<OInfo> fisql(int id, int count) {
		String hql = "from OInfo o where o.del='0' and o.columnId=" + id + " and o.publish=" + "1" + " order by o.id desc";
		return this.personalofficeDao.findListByHqlTop(hql, count);
	}

	public List<ONotice> noticeList(String orgs, int count, boolean role) {
		String hql = "from ONotice";
		if (!role) {
			hql = hql + " where orgId in(" + orgs + ")";
		}
		hql = hql + " order by createTime desc";
		return this.personalofficeDao.findListByHqlTop(hql, count);
	}

	public List<OMailReceiver> fihql(String id) {
		String hql = "from OMailReceiver o where o.del='0'  and o.receiverId in(" + id + ") order by o.inMail.createTime desc,o.ifRead asc";
		return this.personalofficeDao.findListByHqlTop(hql, 6);
	}

	public void deleteGroup(String[] ids) {
		if ((ids != null) && (ids.length > 0)) {
			String group = "";
			for (int i = 0; i < ids.length; i++) {
				if ("".equals(group))
					group = group + ids[i];
				else {
					group = group + "," + ids[i];
				}
			}
			String hql = "delete from OWorkGroup where id in(" + group + ")";
			this.personalofficeDao.executeHql(hql);
			String hql1 = "delete from OGroupUser where groupId in (" + group + ")";
			this.personalofficeDao.executeHql(hql1);
		}
	}

	public List<OUser> getgroupuse(int grid) {
		String hql = "from OUser t where t.id in (select o.userId from OGroupUser o where o.groupId=" + grid + ")";
		return this.personalofficeDao.findByHql(hql);
	}

	public PersonalofficeDao getPersonalofficeDao() {
		return this.personalofficeDao;
	}

	public void setPersonalofficeDao(PersonalofficeDao personalofficeDao) {
		this.personalofficeDao = personalofficeDao;
	}

	public UserService getUserService() {
		return this.userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void saveFax(OFax fax) {
		this.personalofficeDao.saveFax(fax);
		String[] ids = fax.getReceiverIds().split(",");
		for (int i = 0; i < ids.length; i++) {
			OFaxReceive faxReceive = new OFaxReceive();
			faxReceive.setReceiveId(Integer.valueOf(ids[i]));
			faxReceive.setDel("0");
			faxReceive.setIfRead("0");
			faxReceive.setOFax(fax);
			this.personalofficeDao.saveFaxReceive(faxReceive);
		}
	}

	public void saveFaxReceive(OFaxReceive faxReceive) {
		this.personalofficeDao.saveFaxReceive(faxReceive);
	}

	public List<OFaxReceive> getMyFax(OUser user, String title, String startDate, String endDate, int currentPage) {
		String hql = "from OFaxReceive o where o.del='0' and o.receiveId=" + user.getId();
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.OFax.title like '%" + title.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and o.OFax.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and o.OFax.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		hql = hql + " order by o.ifRead,o.id desc";
		return this.personalofficeDao.findByPage(hql, currentPage);
	}

	public long getMyFaxCount(OUser user, String title, String startDate, String endDate) {
		String hql = "select count(id) from OFaxReceive o where o.del='0' and o.receiveId=" + user.getId();
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.OFax.title like '%" + title.trim() + "%'";
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			hql = hql + " and o.OFax.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			hql = hql + " and o.OFax.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		return this.personalofficeDao.countTotalByHql(hql).longValue();
	}

	public OFaxReceive getFax(String id) {
		String hql = "from OFaxReceive o where o.id=" + id;
		return (OFaxReceive) this.personalofficeDao.findByHql(hql).get(0);
	}

	public void deleteFax(String[] ids) {
		if ((ids != null) && (ids.length > 0)) {
			String group = "";
			for (int i = 0; i < ids.length; i++) {
				if ("".equals(group))
					group = group + ids[i];
				else {
					group = group + "," + ids[i];
				}
			}
			String hql = "update OFaxReceive o set o.del='1' where o.id in(" + group + ")";
			this.personalofficeDao.executeHql(hql);
		}
	}
}

/*
 * Location: /Volumes/work/oa/WEB-INF/classes/ Qualified Name:
 * com.jcrj.service.PersonalofficeService JD-Core Version: 0.6.0
 */